struct Solution {}
impl Solution {
  pub fn plates_between_candles(s: String, queries: Vec<Vec<i32>>) -> Vec<i32> {
    let mut index = vec![];
    let mut val = vec![];
    let mut cnt = 0;
    let mut flag = false;
    for (i, c) in s.chars().enumerate() {
      match c {
        '*' => {
          cnt += 1;
        }
        '|' => {
          if !flag {
            flag = true;
            cnt = 0;
            index.push(i);
            val.push(0)
          } else {
            index.push(i);
            val.push(cnt + val[val.len() - 1]);
            cnt = 0;
          }
        }
        _ => {}
      }
    }
    if index.len() <= 1 {
      return vec![0; queries.len()] 
    }
    if s.ends_with("*") {
      index.push(s.len() - 1);
      val.push(val[val.len() - 1]);
    }
    // println!("{:?} {:?}", index, val);
    queries.iter().map(|q| {
      let mut r = index.binary_search(&(q[1] as usize)).unwrap_or_else(|x| x);
      let l= index.binary_search(&(q[0] as usize)).unwrap_or_else(|x| x);
      if r > l && index[r] != q[1] as usize {
        r -= 1;
      }
      val[r] -  val[l]
    }).collect()
  }
}